package org.casper.json;

import java.util.ArrayList;
import java.util.List;

import org.casper.web.User;

import com.alibaba.fastjson.JSON;

public class TestJSON {

	public static void main(String[] args) {
		/**
		 * java bean to JSON
		 * List<Java Bean> to JSON
		 * List<String> to JSON
		 * List<Map<String,Object>> to JSON
		 */
		User user = new User(1,"casper","5146");
		String userJson = JSON.toJSONString(user);
		System.out.println(userJson);
		
		ArrayList<User> allUsers = new ArrayList<User>();
		allUsers.add(new User(1,"casper","5146"));
		allUsers.add(new User(2,"cherry","605143"));
		String allUserJson = JSON.toJSONString(allUsers);
		System.out.println(allUserJson);
		
		
		/**
		 * beanJson to bean 
		 * beabJson to List<JavaBean>
		 * 
		 */
		String str ="{\"id\":1,\"name\":\"casper\",\"pass\":\"5146\"}";
		User u1 = JSON.parseObject(str, User.class);
		System.out.println(u1);
		
		List<User> users = JSON.parseArray(allUserJson, User.class);
		for (User user2 : users) {
			System.out.println(user2);
		}
		
		
		
	}
}
